package com.cysd.ewmksbs.ui.activity

import android.os.Bundle
import com.airbnb.lottie.LottieDrawable
import com.cszy.ewmxns.base.BaseVMActivity
import com.cszy.ewmxns.ext.launch

import com.cysd.ewmksbs.viewmodel.MainViewModel
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.csylhl.ewmksb.R
import com.csylhl.ewmksb.databinding.ActivityCreatQrcodeProGressBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class LoadingQrcodeProGressActivity : BaseVMActivity<MainViewModel, ActivityCreatQrcodeProGressBinding>(),IAdViewListener {

      var content:String?= null
    private var progressJob: Job? = null
    private var isPaused = false
    private var currentProgress = 0
    private val progressChannel = Channel<Unit>() // 用于恢复信号
    var type_proces: String? = null //功能入口类型
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.headTitle.headBack.setImageResource(R.mipmap.ic_back)
        mBinding.headTitle.headRelative.setBackgroundColor(resources.getColor(R.color.black))
    }

    override fun initData() {
        content=intent.getStringExtra("scanResult")
        mBinding.lottieAnimationView.repeatCount = LottieDrawable.INFINITE // 无限循环
        mBinding.lottieAnimationView.playAnimation()
        startProgressWithPauses()
    }

    override fun initListener() {

        mBinding.headTitle.headBack.setOnClickListener {
            finish()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        mBinding.lottieAnimationView.cancelAnimation()
        progressJob?.cancel()
    }

    private fun startProgressWithPauses() {
        progressJob?.cancel() // 取消之前的任务
        progressJob = CoroutineScope(Dispatchers.Main).launch {
            (1..100).forEach { progress ->
                // 等待恢复信号（如果暂停）
                if (isPaused) {
                    progressChannel.receive() // 挂起直到收到继续信号
                }

                currentProgress = progress
                updateProgress(progress)
                delay(50) // 5秒总时长

                // 检查暂停点
                when (progress) {
                    40 -> showVideo()
                    80 -> showCHApin()
                    100 -> goResult() // 进度完成监听
                }
            }
        }

    }

    private fun updateProgress(progress: Int) {
      //  progressBar.progress = progress
       mBinding.tvProgress .text = "$progress%"
    }


    //激励视频
    private fun showVideo() {
        type_proces="激励视频"
        isPaused = true
        AdManager.loadVideo_nocache(
            this,
            2,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    isPaused = false
                    progressChannel.trySend(Unit) // 发送继续信号
                }
            })
    }
    //插屏
    private fun showCHApin() {
        type_proces="插屏"
        isPaused = true


        AdManager.loadChapin_nocache(
            this,
            2,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    isPaused = false
                    progressChannel.trySend(Unit) // 发送继续信号
                }
            })
    }
    private fun goResult(){
        var bundle=Bundle()
        bundle.putString("scanResult",content)
        launch(bundle,Ksb_ScanResultActivity::class.java)
        finish()
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
        isPaused = false
        progressChannel.trySend(Unit) // 发送继续信号
    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
}